package edu.sctu.xialei.june.day20150614one.file.test;

import java.io.*;
import java.util.Scanner;

/**
 * Created by Administrator on 2015/6/14.
 */


/*IO FileInputStream

 */
public class IO_Util {

    //    input the stream
    public void fileInputStream() {

        File file = new File(new Scanner(System.in).next());

        FileInputStream inputStream = null;

        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int value;
        if (inputStream != null) {
            try {
                while ((value = inputStream.read()) != -1) {
                    System.out.println(value);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    //    output(copy) the stream
    public void fileOutputStream() {
        System.out.println("\n*** type a file path to input it by int json_data ***");
        File fileIn = new File(new Scanner(System.in).next());

        System.out.println("\n*** type a file path to output it by int json_data ***");
        File fileOut = new File(new Scanner(System.in).next());
        try {
            fileOut.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(fileOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            inputStream = new FileInputStream(fileIn);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        int value;
        if (inputStream != null) {
            try {
                while (inputStream.read() != -1) {
                    outputStream.write(inputStream.read());
                }
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //    copy by byte[]
    public void fileOutputStream_ByByteArray() {

        System.out.println("\n*** type a file path to input it by int json_data ***");
        File fileIn = new File(new Scanner(System.in).next());

        System.out.println("\n*** type a file path to output it by int json_data ***");
        File fileOut = new File(new Scanner(System.in).next());
        try {
            fileOut.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(fileOut);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            inputStream = new FileInputStream(fileIn);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        byte[] array = new byte[20480];

        try {
            if (outputStream != null) {
                outputStream.write(inputStream.read(array));
            }

            inputStream.close();

            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    input the stream and print by char or String
    public void fileInputStream_Print() {

        File file = new File(new Scanner(System.in).next());

        FileInputStream inputStream = null;

        try {
            inputStream = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

//        print by char

        int value;
        if (inputStream != null) {
            try {
                while ((value = inputStream.read()) != -1) {
                    System.out.println((char) value);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


//        print by array
        char[] values = new char[30];
        if (inputStream != null) {
            try {
                int count = 0;
//                int value = 0;
                while ((value = inputStream.read()) != -1) {
                    values[count] = (char) value;
                    count++;
                }
                System.out.println(values);

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    //    fileReader
    public void fileReaderPractice() {
        File file = new File(new Scanner(System.in).next());

        FileReader fileReader = null;

        try {
            int count;
            fileReader = new FileReader(file);
            while (( count = fileReader.read() ) != -1) {
                System.out.println(count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //    fileWrite
    public void fileWritePractice() {
        File fileIn = new File(new Scanner(System.in).next());
        File fileOut = new File(new Scanner(System.in).next());

        FileReader fileReader = null;
        FileWriter fileWriter = null;

        try {
            short value = 0;

            fileReader = new FileReader(fileIn);
            fileWriter = new FileWriter(fileOut);

            while (  ( value = (short) fileReader.read())  != -1) {

                fileWriter.write(value);
            }
            fileReader.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

